package th.kmutt.se.is.model;

import java.util.ArrayList;
import java.util.List;

import th.kmutt.se.is.fact.Database;
import android.content.Context;
import android.database.Cursor;

public class Charges {
	private int id;
	private int type;
	private String name;
	private String desc;
	private double defaultPrice;
	private int unitUse;
	private int status;

	private Context context;
	private Database db;
	
	final String[] cols = new String[] {
				Database.KEY_ID, Database.KEY_TYPE, 
				Database.KEY_NAME, Database.KEY_DESC, 
				Database.KEY_DPRICE, Database.KEY_STATUS
			};
	
	public Charges(Context c) {
		context = c;
		db = new Database(c);
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public double getDefaultPrice() {
		return defaultPrice;
	}
	public void setDefaultPrice(double defaultPrice) {
		this.defaultPrice = defaultPrice;
	}
	public int getUnitUse() {
		return unitUse;
	}
	public void setUnitUse(int unitUse) {
		this.unitUse = unitUse;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
	public boolean create(){return true;}
	public boolean delete(){return true;}
	public boolean update(){return true;}
	
	public Charges getCharges(){return new Charges(context);}
	public Charges[] getChargesList(int status){
		db.openToRead();
		List<Charges> chargesList = new ArrayList<Charges>();
		Cursor cursor;
		
		if(status>0){
			cursor = db.sqLiteDatabase.query(Database.TB_CHARGES, cols, cols[5]+"="+status, null, null, null, null);
		}else{
			cursor = db.sqLiteDatabase.query(Database.TB_CHARGES, cols, null, null, null, null, null);
		}
		
		if(cursor!=null && cursor.getCount()>0){
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()){
				Charges charges = new Charges(context);
				charges.setId(cursor.getInt(cursor.getColumnIndex(cols[0])));
				charges.setType(cursor.getInt(cursor.getColumnIndex(cols[1])));
				charges.setName(cursor.getString(cursor.getColumnIndex(cols[2])));
				charges.setDesc(cursor.getString(cursor.getColumnIndex(cols[3])));
				charges.setDefaultPrice(cursor.getDouble(cursor.getColumnIndex(cols[4])));
				charges.setStatus(cursor.getInt(cursor.getColumnIndex(cols[5])));
				chargesList.add(charges);
			}
			cursor.close();
			db.close();
		}else{
			cursor.close();
			db.close();
			return null;
		}
		
		return chargesList.toArray(new Charges[chargesList.size()]);
	}
	public Charges[] getChargesListByRoomId(int roomId){return new Charges[5];}
	
}
